/**
 * Social Ratings
 * Implements the same CRUD terminology
 *
 * @author Seth
 * @version 0.5
 */
Social.Ratings = function(){
	var s = {},
		page = 1,

		// create (C)
		_create = function() {
	    	Petolio.ajax({
				url: 'social/create',
				type: 'post',
				data: {id: s.id, scope: s.scope, plugin: 'ratings', label: s.label, url: s.url, owner: s.owner},
				cache: false,
				success: function (x) {
					_read();
				}
			});

		// read (R)
		},	_read = function() {
	    	Petolio.ajax({
				url: 'social/read',
				type: 'post',
				data: {id: s.id, scope: s.scope, plugin: 'ratings'},
				cache: false,
				success: function (x) {
					s.loc.html(x.html);
					_bind();
					Social.done();
				}
			});

		// update (U)
		},	_update = function() {
			// not used

		// delete (D)
		},	_delete = function(e) {
			var id = $(this).attr('rel');

	    	Petolio.ajax({
				url: 'social/delete',
				type: 'post',
				data: {x: id, id: s.id, scope: s.scope, plugin: 'ratings'},
				cache: false,
				success: function (x) {
					_read();
				}
			});

		// additional view event
		},	_view = function() {
	    	Petolio.showLoading();
	    	Petolio.ajax({
				url: 'social/misc',
				type: 'post',
				data: {id: s.id, scope: s.scope, plugin: 'ratings', page: page},
				cache: false,
				success: function (x) {
					Petolio.hideLoading();
					_likes(x, _translate, _pagination, function(){
						page = 1;
					});
				}
			});

		// likes dialog (common, also used on the dashboard)
		},	_likes = function(x, t, p, c) {
	    	var identity = 'dialog-likes';
	    	$("#"+ identity).remove();

	    	$('<div id="'+ identity +'" title="'+ t('view_ratings') +'" class="ui-state-highlight">'+
				'<div style="position: relative; margin: 8px 0px 3px 0px;">' +
					t(x.pattern + '_' + (x.total == 1 ? 1 : 2)).replace('%1$s', x.total).replace('%2$s', x.total == 1 ? t('person') : t('people')) +
					x.html +
				'</div>'+
			'</div>').dialog({
				resizable: false,
				modal: true,
				width: 500,
				minHeight: 50,
				buttons: [{
					text: t('close'),
					click: function() {
						$(this).dialog("close");
					}
				}],
				close: function() {
					$("#"+ identity).remove();
					if(c)
						c.apply(this, Array.prototype.slice.call(arguments, 4));
				},
				open: function() {
					$("#"+ identity).find('[class="rating_pagination"] a').click(p);
					$("#"+ identity).find('a').blur();
				}
			});

		// pagination handle
		},	_pagination = function() {
			var p = parseInt($(this).attr('rel'));

			// only numbers allowed
			if(!isNaN(p))
				page = p;

			_view();

		// bind events
		},	_bind = function() {
			s.loc.find('[class="rating_add"]').click(_create);
			s.loc.find('[class="rating_remove"]').click(_delete);
			s.loc.find('[class="rating_view"]').click(_view);

		// translation handle
		},	_translate = function(i) {
			var t = s.loc.find('[class="rating_translate"]'),
				o = $.parseJSON(t.html());

			return o[i];

		// plugin constructor
		},	__construct = function(win, params) {
			s.loc = win.find('.ratings');
			s.id = params.id;
			s.scope = params.scope;
			s.label = params.label;
			s.url = params.url;
			s.owner = params.owner;
			s.win = win;

			_read();
		};

	// public
	return {
		load: __construct,
		likes: _likes
	};
}();